//
//  DetailViewController.m
//  OSClient
//
//  Created by Chaosky on 15/10/23.
//  Copyright © 2015年 1000phone. All rights reserved.
//

#import "DetailViewController.h"
#import <Masonry.h>
#import "Utils.h"
#import "OSCNewsDetail.h"

@interface DetailViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) NSString * detailUrl;
@property (nonatomic, strong) id originObject; // 对应着初始化视图控制器参数的对象

@property (nonatomic, strong) UIWebView * detailWebView;

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    [self requestDetail];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - 重写初始化方法

- (instancetype)initWithNews:(OSCNews *)news
{
    if (self = [super init]) {
        self.originObject = news;
    }
    return self;
}

#pragma mark - UI相关
- (void)setupUI
{
    // 创建UIWebView
    self.detailWebView = [[UIWebView alloc] init];
    [self.view addSubview:self.detailWebView];
    self.detailWebView.delegate = self;
    [self.detailWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
}


#pragma mark - 请求数据

// 分发数据
- (void)requestDetail {
    [KVNProgress showWithStatus:@"详情加载中..."];
    // 新闻详情
    if ([self.originObject isKindOfClass:[OSCNews class]]) {
        OSCNews * news = self.originObject;
        [self requestNewsDetailWithID:news.ID];
    }
    else
    {
        
    }
}

// 请求新闻详情
- (void)requestNewsDetailWithID:(NSString *) ID
{
    // 创建网络请求对象
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager OSCManager];
    // 拼接URL请求地址
    NSString * url = [NSString stringWithFormat:@"%@/%@?id=%@", OSCAPI_PREFIX, OSCAPI_NEWS_DETAIL, ID];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, ONOXMLDocument * doument) {
        // 处理XMLDoument
        ONOXMLElement * newsElement = [doument.rootElement firstChildWithTag:@"news"];
        // 通过数据模型解析
        OSCNewsDetail * newsDetail = [[OSCNewsDetail alloc] initWithXMLElement:newsElement];
        [self setupNewsDetail:newsDetail];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

// 加载新闻详情
- (void)setupNewsDetail:(OSCNewsDetail *) newsDetail
{
    self.title = @"新闻详情";
    // HTML/CSS/JavaScript 网页三剑客
    // HTML 显示 类似iOS中的UI部分
    // CSS样式表：定义样式，类似iOS中的属性值color,font
    // JavaScript：脚本语言，定义交互，类似iOS中的action-target事件响应
    [self.detailWebView loadHTMLString:newsDetail.html baseURL:[[NSBundle mainBundle] resourceURL]];
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [KVNProgress showSuccessWithStatus:@"加载成功"];
}

@end





